home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wpj1_8.zip / HELLO.ZIP / HELLO.C < prev    next >
C/C++ Source or Header  |  1993-08-23  |  17KB  |  588 lines

  1. /*============================================================================
  2.   Hello - Windows Programmer's Journal Volume 1 Number 8
  3.  
  4.   File      : Hello.C
  5.  
  6.   Prototype : 
  7.  
  8.   Call      : 
  9.  
  10.   Library   : 
  11.  
  12.   Example   : 
  13.  
  14.   22 August 1993 - dlcampbell
  15.   1993 Dave Campbell WynApse
  16. ----------------------------------------------------------------------------*/
  17. #define WINVER 0x0300
  18. #include <windows.h>
  19. #include "hello.h"
  20. #include <string.h>
  21. #include <commdlg.h>
  22.  
  23. char       szAppName[] = "Hello";
  24. HANDLE     hInst;
  25. HWND       hWndMain;
  26. HANDLE     hCommDlg;
  27. HICON      hIcon, hIconMain;
  28. int        InitSettings;
  29. static     HMENU hMainMenu, hAlternateMenu;
  30.  
  31. static     char szFileName[256];
  32. static     WORD wFileAttr;
  33.  
  34. COLORREF   ColorRef;
  35. HANDLE     MyFont = NULL;
  36.  
  37. /*----------------------------------------------------------------------------
  38.   Function Prototypes
  39. ----------------------------------------------------------------------------*/
  40. BOOL FAR PASCAL AboutDlgProc(HWND, WORD, WORD, LONG);
  41.  
  42. BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  43. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  44. BOOL            InitInstance(HANDLE);
  45. BOOL FAR PASCAL TimerProc(HWND, WORD, WORD, LONG);
  46.  
  47. LPSTR lstrchr (LPSTR str, char ch);
  48.  
  49. /* The main procedure. This is called by Windows when your program is run. */
  50. /* hInstance is the handle for the current instance of the program         */
  51. /* hPrevInst is the handle for the last instance of the program to run     */
  52. /* CmdLine is a pointer to a string of whatever command line params came in*/
  53. /* nCmd is the specifies the applications appearance.                      */
  54.  
  55. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInst, LPSTR CmdLine,
  56.                                                                      int nCmd)
  57. {
  58. MSG     msg;                                /* Message */
  59. HWND    hWnd;                       /* Our Window Handle */
  60. HMENU   hMenu;
  61.  
  62. if (!hPrevInst)                         /* If no instance already exists, */
  63.    {
  64.     if (!InitInstance(hInstance))       /* try initializing instance */
  65.         return FALSE;                     /* No dice, it failed */
  66.     }
  67.  
  68. hInst = hInstance;
  69.     
  70. hWndMain = CreateWindow(szAppName,              /* Window Class */
  71.                               "Hello World Program",  /* Window caption */
  72.                               WS_OVERLAPPEDWINDOW,    /* Overlapped style */
  73.                               CW_USEDEFAULT,          /* Use defaults for the */
  74.                               CW_USEDEFAULT,          /* X & Y Positions and  */
  75.                               CW_USEDEFAULT,          /* X & Y sizes */
  76.                               CW_USEDEFAULT,          
  77.                               NULL,                   /* Parent Window */
  78.                               NULL,
  79.                               hInstance,              /* Instance */
  80.                               NULL);                  /* Creation Params */
  81.  
  82. if (hWndMain == NULL)
  83.    return 0;
  84.  
  85. hCommDlg = LoadLibrary("COMMDLG.DLL");
  86. if (hCommDlg < 32)
  87.    {
  88.    FreeLibrary(hCommDlg);
  89.    MessageBox(GetFocus(),"Dynamic Link Library COMMDLG.DLL must be present",
  90.                                        szAppName, MB_ICONEXCLAMATION | MB_OK);
  91.    return 0;
  92.    }
  93.  
  94. SetTimer(hWndMain, ID_TIMER, 500, MakeProcInstance((FARPROC)TimerProc, hInst));
  95.  
  96. hMenu = GetSystemMenu(hWndMain, FALSE);
  97. hAlternateMenu = LoadMenu(hInstance, "Hello2");
  98.  
  99. AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
  100. AppendMenu(hMenu, MF_STRING,    IDM_HELPABOUT, "About...");
  101. AppendMenu(hMenu, MF_STRING,    IDM_SETUP, "Setup...");
  102.  
  103. ShowWindow(hWndMain, nCmd);
  104. UpdateWindow(hWndMain);
  105.     
  106. while (GetMessage(&msg, NULL, 0, 0))
  107.    {
  108.     TranslateMessage(&msg);
  109.     DispatchMessage(&msg);
  110.    }
  111.     
  112. if (MyFont != NULL)
  113.    DeleteObject(MyFont);
  114.  
  115. FreeLibrary(hCommDlg);
  116. return msg.wParam;
  117. }                                       /* int FAR PASCAL WinMain */
  118.  
  119. BOOL InitInstance (HANDLE hInstance)
  120. {
  121. WNDCLASS wc;                            /* Window class structure */
  122.  
  123. wc.lpszMenuName  = szAppName;
  124. wc.lpszClassName = szAppName;
  125. wc.hbrBackground = GetStockObject(WHITE_BRUSH);  /* White bkgrnd */
  126. wc.hInstance     = hInstance;
  127. wc.style         = CS_VREDRAW | CS_HREDRAW;
  128. wc.lpfnWndProc   = (WNDPROC)WndProc;              /* Name of proc to handle window */
  129. wc.hCursor       = LoadCursor(NULL, IDC_ARROW);  /* Arrow Cursor */
  130. wc.hIcon         = LoadIcon(hInstance, "WPJ");  /* Generic Icon */
  131. wc.cbClsExtra    = 0;                /* no extras */
  132. wc.cbWndExtra    = 0;                /* No Extras */
  133.  
  134. hIconMain = wc.hIcon;
  135.  
  136. return (RegisterClass(&wc)); /* Let's register that class */
  137. }                                       /* BOOL InitInstance */
  138.  
  139. /* This is our main windows procedure. It receives messages in message, then,
  140. depending on what the message is, we react accordingly
  141. */
  142.  
  143. long FAR PASCAL WndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  144. {
  145. HDC            hdc;
  146. PAINTSTRUCT    ps;
  147. RECT           rect;
  148. HMENU          hMenu;
  149. static FARPROC lpfnHelloDlgProc, lpfnAboutDlgProc;
  150. char           OutMsg[25];
  151.  
  152. OPENFILENAME   openfile;
  153. char           szFilter[25];
  154. char           szExt[4 + 1];
  155. char           szDirName[256];
  156. char           szFileSpec[256];
  157. char           szDefExt[5];
  158. char           szFileTitle[128];
  159. int            i;
  160.  
  161. COLORREF       LocColorRef[16];
  162. CHOOSECOLOR    ChooseMyColor;
  163.  
  164. DWORD          dwFlags;
  165. CHOOSEFONT     ChooseMyFont;
  166. LOGFONT        LogMyFont;
  167.  
  168. HANDLE         TempFont;
  169.  
  170. lstrcpy(OutMsg, "");
  171.  
  172. switch (message)
  173.    {
  174.    case WM_CREATE :
  175.       ColorRef = RGB(256, 256, 256);
  176.       hMainMenu = GetMenu(hWnd);
  177.       InitSettings = GetPrivateProfileInt("Hello", "Setup", 1, "Hello.ini");
  178.       InitSettings = InitSettings ? IDM_HELLO : IDM_GOODBYE;
  179.       hdc = GetDC(hWnd);
  180.          InvalidateRect(hWnd, NULL, TRUE);
  181.       ReleaseDC(hWnd, hdc);
  182.  
  183. /*----------------------------------------------------------------------------
  184.   fall through to WM_PAINT...
  185. ----------------------------------------------------------------------------*/
  186.    case WM_PAINT :
  187.       if (IsIconic(hWnd))
  188.          {
  189.          BeginPaint(hWnd, &ps);
  190.  
  191. /*----------------------------------------------------------------------------
  192.   Erase the background of the window with what's on the desktop. This is so
  193.   the desktop bitmap will show through the transparent areas of the icon. 
  194. ----------------------------------------------------------------------------*/
  195.          DefWindowProc(hWnd, WM_ICONERASEBKGND, (WORD)ps.hdc, 0L);
  196.  
  197. /*----------------------------------------------------------------------------
  198.   Now draw the icon. 
  199. ----------------------------------------------------------------------------*/
  200.          DrawIcon(ps.hdc, 0,0, hIcon);
  201.          EndPaint(hWnd, &ps);
  202.          }
  203.       else
  204.          {
  205.          hdc = BeginPaint(hWnd,&ps);        /* returns pointer to hdc */
  206.          GetClientRect(hWnd, &rect);
  207. /*
  208.   -1 tells the DrawText function to calculate length of string based on
  209.   NULL-termination
  210. */
  211.          SetTextColor(hdc, ColorRef);
  212.  
  213.          if (MyFont != NULL)
  214.             TempFont = SelectObject(hdc, MyFont);
  215.  
  216.          DrawText(hdc, (InitSettings == IDM_HELLO) ? "Hello Windows!" : "Goodbye Windows!", -1,
  217.                                    &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  218.          if (MyFont != NULL)
  219.             SelectObject(hdc, TempFont);
  220.  
  221.          EndPaint(hWnd,&ps);
  222.          }
  223.       return 0;
  224.  
  225.    case WM_SYSCOMMAND :
  226.       switch (wParam)
  227.          {
  228.          case IDM_SETUP :
  229.             lpfnHelloDlgProc = MakeProcInstance(HelloDlgProc, hInst);
  230.             DialogBox(hInst, "Hello", hWnd, lpfnHelloDlgProc);
  231.             FreeProcInstance(lpfnHelloDlgProc);
  232.             return 0;
  233.  
  234.          case IDM_HELPABOUT :
  235.             lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  236.             DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  237.             FreeProcInstance(lpfnAboutDlgProc);
  238.             return 0;
  239.          }
  240.  
  241.       break;
  242.  
  243.  
  244.    case WM_ERASEBKGND:
  245.       if (IsIconic(hWnd))
  246.  
  247. /*----------------------------------------------------------------------------
  248.   Don't erase the background now because we will do it at paint time when
  249.   we paint our icon.
  250. ----------------------------------------------------------------------------*/
  251.          return(TRUE);
  252.       else
  253.          return(DefWindowProc(hWnd, message, wParam, lParam));
  254.  
  255.    case WM_QUERYDRAGICON:
  256.       return((LONG)(WORD)hIconMain);
  257.  
  258.    case WM_COMMAND :
  259.       hMenu = GetMenu(hWnd);
  260.       switch (wParam)
  261.          {
  262.       case IDM_FILENEW :
  263.          lstrcpy(OutMsg, "IDM_FILENEW");
  264.          break;
  265.  
  266.       case IDM_FILEOPEN :
  267.          memset(&openfile, 0, sizeof(OPENFILENAME));
  268.  
  269.          openfile.lStructSize = sizeof(OPENFILENAME);
  270.          openfile.hwndOwner = hWnd;
  271.          strcpy(szFilter, "Text Files|*.txt|");
  272.  
  273.          for (i = 0; szFilter[i] != '\0'; i++)
  274.             {
  275.             if (szFilter[i] == '|')
  276.                szFilter[i] = '\0';
  277.             }   
  278.          openfile.lpstrFilter = (LPSTR)szFilter;
  279.          openfile.nFilterIndex = 1;
  280.  
  281.          szFileName[0] = '\0';
  282.          openfile.lpstrFile= (LPSTR)szFileName;
  283.          openfile.nMaxFile = sizeof(szFileName);
  284.          openfile.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  285.  
  286.          GetOpenFileName(&openfile);
  287.          lstrcpy(OutMsg, openfile.lpstrFile);
  288.          break;
  289.  
  290.       case IDM_FILESAVE :
  291.          lstrcpy(OutMsg, "IDM_FILESAVE");
  292.          break;
  293.  
  294.       case IDM_FILESAVEAS :
  295.          lstrcpy(OutMsg, "IDM_FILESAVEAS");
  296.          break;
  297.  
  298.       case IDM_FILEPRINT :
  299.          lstrcpy(OutMsg, "IDM_FILEPRINT");
  300.          break;
  301.  
  302.       case IDM_FILEPRINTPREV :
  303.          lstrcpy(OutMsg, "IDM_FILEPRINTPREV");
  304.          break;
  305.  
  306.       case IDM_FILEPRINTSETUP :
  307.          lstrcpy(OutMsg, "IDM_FILEPRINTSETUP");
  308.          break;
  309.  
  310.       case IDM_FILEEXIT :
  311.          lstrcpy(OutMsg, "IDM_FILEEXIT");
  312.          break;
  313.  
  314.  
  315.       case IDM_EDITUNDO :
  316.          lstrcpy(OutMsg, "IDM_EDITUNDO");
  317.          break;
  318.  
  319.       case IDM_EDITREPEAT :
  320.          lstrcpy(OutMsg, "IDM_EDITREPEAT");
  321.          break;
  322.  
  323.       case IDM_EDITCUT :
  324.          lstrcpy(OutMsg, "IDM_EDITCUT");
  325.          break;
  326.  
  327.       case IDM_EDITCOPY :
  328.          lstrcpy(OutMsg, "IDM_EDITCOPY");
  329.          break;
  330.  
  331.       case IDM_EDITPASTE :
  332.          lstrcpy(OutMsg, "IDM_EDITPASTE");
  333.          break;
  334.  
  335.       case IDM_EDITCLEAR :
  336.          lstrcpy(OutMsg, "IDM_EDITCLEAR");
  337.          break;
  338.  
  339.       case IDM_EDITPAST :
  340.          lstrcpy(OutMsg, "IDM_EDITPAST");
  341.          break;
  342.  
  343.  
  344.       case IDM_SAMPLEDLG :
  345.          lstrcpy(OutMsg, "IDM_SAMPLEDLG");
  346.          break;
  347.  
  348.       case IDM_COLOR : 
  349.          memset(&ChooseMyColor, 0, sizeof(CHOOSECOLOR));
  350.          ChooseMyColor.lStructSize=sizeof(CHOOSECOLOR);
  351.          ChooseMyColor.lpCustColors=LocColorRef;
  352.          ChooseMyColor.hwndOwner=hWnd;
  353.          ChooseMyColor.rgbResult = ColorRef;
  354.          ChooseMyColor.Flags = CC_RGBINIT;
  355.  
  356.          if (ChooseColor(&ChooseMyColor))
  357.             {
  358.             ColorRef = ChooseMyColor.rgbResult;
  359.             InvalidateRect(hWnd, NULL, TRUE);
  360.             UpdateWindow(hWnd);
  361.             }
  362.          break;
  363.  
  364.       case IDM_FONT : 
  365.          memset(&ChooseMyFont, 0, sizeof(CHOOSEFONT));
  366.          ChooseMyFont.lStructSize = sizeof(CHOOSEFONT);
  367.          ChooseMyFont.hwndOwner = hWnd;
  368.          ChooseMyFont.lpLogFont = &LogMyFont;
  369.          lstrcpy(LogMyFont.lfFaceName, "Times New Roman");
  370.          LogMyFont.lfHeight = 10;
  371.          LogMyFont.lfItalic = FALSE;
  372.          LogMyFont.lfWeight = FW_BOLD;
  373.          LogMyFont.lfStrikeOut = FALSE;
  374.          LogMyFont.lfUnderline = FALSE;
  375.          ChooseMyFont.rgbColors = ColorRef;
  376.  
  377.          ChooseMyFont.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT;
  378.  
  379.          if (ChooseFont(&ChooseMyFont))
  380.             {
  381.             if (MyFont != NULL)
  382.                DeleteObject(MyFont);
  383.  
  384.             MyFont = CreateFontIndirect(&LogMyFont);
  385.             ColorRef = ChooseMyFont.rgbColors;
  386.  
  387.             InvalidateRect(hWnd, NULL, TRUE);
  388.             UpdateWindow(hWnd);
  389.             }
  390.          break;
  391.  
  392.       case IDM_HELPINDX :
  393.          lstrcpy(OutMsg, "IDM_HELPINDX");
  394.          break;
  395.  
  396.       case IDM_HELPKBD :
  397.          lstrcpy(OutMsg, "IDM_HELPKBD");
  398.          break;
  399.  
  400.       case IDM_HELPCMDS :
  401.          lstrcpy(OutMsg, "IDM_HELPCMDS");
  402.          break;
  403.  
  404.       case IDM_HELPPROCS :
  405.          lstrcpy(OutMsg, "IDM_HELPPROCS");
  406.          break;
  407.  
  408.       case IDM_HELPUSING :
  409.          lstrcpy(OutMsg, "IDM_HELPUSING");
  410.          break;
  411.  
  412.       case IDM_HELPABOUT :
  413.          lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc, hInst);
  414.          DialogBox(hInst, "About", hWnd, lpfnAboutDlgProc);
  415.          FreeProcInstance(lpfnAboutDlgProc);
  416.          return 0;
  417.  
  418.       case IDM_ALTERNATE :
  419.          SetMenu(hWnd, hAlternateMenu);
  420.          return 0;
  421.  
  422.       case IDM_ALTONE : 
  423.          CheckMenuItem(hMenu, IDM_ALTONE, MF_CHECKED);
  424.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_UNCHECKED);
  425.          return 0;
  426.  
  427.       case IDM_ALTTWO : 
  428.          CheckMenuItem(hMenu, IDM_ALTTWO, MF_CHECKED);
  429.          CheckMenuItem(hMenu, IDM_ALTONE, MF_UNCHECKED);
  430.          return 0;
  431.  
  432.       case IDM_ALTGRAY : 
  433.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_GRAYED);
  434.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_ENABLED);
  435.          return 0;
  436.  
  437.       case IDM_ALTUNGRAY : 
  438.          EnableMenuItem(hMenu, IDM_ALTUNGRAY, MF_GRAYED);
  439.          EnableMenuItem(hMenu, IDM_ALTGRAY, MF_ENABLED);
  440.          return 0;
  441.  
  442.       case IDM_ALTRESTORE : 
  443.          SetMenu(hWnd, hMainMenu);
  444.          return 0;
  445.  
  446.       case WM_DESTROY :
  447.           PostQuitMessage(0);
  448.           return 0;
  449.           }
  450.  
  451.    if (lstrlen(OutMsg) != 0)
  452.       {
  453.       MessageBox(hWnd, OutMsg, szAppName, MB_ICONEXCLAMATION | MB_OK);
  454.       return 0;
  455.       }
  456.  
  457.    }
  458. return DefWindowProc(hWnd, message, wParam, lParam);
  459. }                                       /* long FAR PASCAL WndProc */
  460.  
  461. /*============================================================================
  462.   HelloDlgProc -- 
  463.  
  464.   File      : Hello.C
  465.  
  466.   Prototype : BOOL FAR PASCAL HelloDlgProc(HWND, WORD, WORD, LONG);
  467.  
  468.   Call      : HelloDlgProc(hDlg, message, wParam, lParam);
  469.  
  470.   Library   : 
  471.  
  472.   24 April 1993 - dlcampbell
  473.   (c) 1993 WynApse
  474. ----------------------------------------------------------------------------*/
  475. BOOL FAR PASCAL HelloDlgProc (HWND hDlg, WORD message, WORD wParam,
  476.                                                                   LONG lParam)
  477. {
  478. switch (message)
  479.    {
  480.    case WM_INITDIALOG :
  481.       CheckRadioButton(hDlg, IDM_HELLO, IDM_GOODBYE, InitSettings);
  482.       return TRUE;
  483.  
  484.    case WM_COMMAND :
  485.       switch (wParam)
  486.          {
  487.          case IDM_HELLO : 
  488.                WritePrivateProfileString("Hello", "Setup", "1", "Hello.ini");
  489.             InitSettings = wParam;
  490.             break;
  491.  
  492.          case IDM_GOODBYE : 
  493.                WritePrivateProfileString("Hello", "Setup", "0", "Hello.ini");
  494.             InitSettings = wParam;
  495.             break;
  496.  
  497.          case IDOK :
  498.             EndDialog(hDlg, wParam);
  499.             return TRUE;
  500.  
  501.          }
  502.       break;
  503.  
  504.    }
  505. return FALSE;
  506. }                                       /* HelloDlgProc */
  507.  
  508. /*============================================================================
  509.   AboutDlgProc
  510.  
  511.   File      : ExitWin.C
  512.  
  513.   Prototype : 
  514.  
  515.   Call      : 
  516.  
  517.   Library   : 
  518.  
  519.   24 April 1993 - dlcampbell
  520.   (c) 1993 WynApse
  521. ----------------------------------------------------------------------------*/
  522. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam,
  523.                                                                   LONG lParam)
  524. {
  525. char szBuffer[100];
  526.  
  527. switch (message)
  528.    {
  529.    case WM_INITDIALOG :
  530.       wsprintf(szBuffer, "%s at %s", (LPSTR) __DATE__, (LPSTR) __TIME__);
  531.       SetWindowText(GetDlgItem(hDlg, ID_VERSION), szBuffer);
  532.       return TRUE;
  533.  
  534.    case WM_COMMAND :
  535.       switch (wParam)
  536.          {
  537.          case IDOK :
  538.          case IDCANCEL :
  539.             if (HIWORD(lParam) == BN_CLICKED)
  540.                EndDialog(hDlg, wParam);
  541.             return TRUE;
  542.  
  543.          default :
  544.             return TRUE;
  545.          }
  546.  
  547.    default :
  548.       return FALSE;
  549.    }
  550. }                                       /* AboutDlgProc */
  551.  
  552. /*============================================================================
  553.   TimerProc
  554.  
  555.   File      : Hello.C
  556.  
  557.   Prototype : 
  558.  
  559.   Call      : 
  560.  
  561.   Library   : 
  562.  
  563.   Example   : 
  564.  
  565.   6 June 1993 - dlcampbell
  566.   (c) 1993 Campbell SoftWare
  567. ----------------------------------------------------------------------------*/
  568. BOOL FAR PASCAL TimerProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
  569. {
  570. static BOOL which = 0;
  571.  
  572. if (IsIconic(hWnd))
  573.    {
  574.    if (which = !which)
  575.       {
  576.       hIcon = LoadIcon(hInst, "GO");
  577.       InvalidateRect(hWnd, NULL, FALSE);
  578.       SendMessage(hWndMain, WM_PAINT, NULL, NULL);
  579.       }
  580.    else
  581.       {
  582.       hIcon = LoadIcon(hInst, "SUNS");
  583.       InvalidateRect(hWnd, NULL, FALSE);
  584.       SendMessage(hWndMain, WM_PAINT, NULL, NULL);
  585.       }
  586.    }
  587. }                                       /* TimerProc */
  588.